var baseUrl = require('../../utils/urllink.js')
import { AreaPicker } from "../areaSelector/selector.js"
var app = getApp()
Page(Object.assign({}, AreaPicker, {
  data: {
    showTopTips: false,
    showTopTipsText: '',
    disabled:false,
    images: [], //上传的图片地址 *
    files: [],
    hostimgs: app.globalData.hostimgs,
    goods_name: '',//  *
    goods_remark: '',//  *
    goods_kuanhao: '',
    cost_price: 0,
    shop_price: 0,//  *
    market_price: 0,//  *
    store_count: 0,//  *
    currentItem: 0, //品牌id *
    fenggecurrentid:0,//风格id
    goods_category:[],//商品分类
    goods_categoryid:0,//商品分类id，3级
    pingpai:[],
    fenggelist:[],//风格list
    shop_zpingpai: '',//商品自定义品牌
    shop_zyanse: '',//商品自定义规格 颜色
    xuanyanse:{},//选择的颜色值id（多选数组）
    xuanyanse_list: [], //选择的颜色name（多选数组）
    xuanchima: {},//选择尺码的值id（多选数组）
    xuanchima_list: [], //选择尺码的name（多选数组）
    shuxing_list: [],//属性集合  选择的属性list  *
    spshuxinginput:'',
    shuxing_list_name:'',
    currentItem1: 0,
    currentItem2: 0,
    currentItem3: 0,

    is_show_guige: false,
    is_show_shuxing: false,
    is_show_chima: false,
    is_show_pingpai:false,
    is_show_fengge: false,
    showModal: false,//弹出框 属性
    showyingxiao:false,//提示弹出框
    sppp:'',

    guigelist: [],////动态规格列表添加 *
    chima1: [],
    chima2:[],
    chima3: [],
    store_id:0,
    province: null,//省 {id,name}
    city: null,//市
    district: null,//县 区
    street: null,//村 街道 


  },

  //显示风格
  showfengge:function(){
    var that = this;
    that.setData({
      is_show_fengge:true,
    })
  },
  //添加用户选择地区完成的回调
  onAreaCommit(locationList) {
    console.log(locationList);
    var that = this
    this.setData({
      province: locationList[0] || {},
      city: locationList[1] || {},
      district: locationList[2] || {},
      street: locationList[3] || {},
    });
    
    //显示添加属性
    this.setData({
      is_show_shuxing: true,
      goods_categoryid: locationList[1].id,
      is_show_guige:true,
    });
    console.log(locationList[1].id)
    //获取商品属性
    getgoods_attr(that, locationList[1].id, that.data.store_id)
    //获取商品尺码规格
    getguige(that, locationList[1].id, that.data.store_id, 2)
    //获取商品规格颜色
    goods_color(that, locationList[1].id, that.data.store_id)
    //获取商品风格
    goods_tags(that, locationList[1].id) 
  },


  //初始化
  onLoad: function (options) {
    var that = this
    console.log("传参store_id="+options.store_id)
    this.setData({
      images: [],
      store_id:options.store_id,
    })

    //判断店铺状态  //调用品牌数据   //商品分类调用数据 pid=0
    get_store_state(that, app.globalData.user_id)


  },


  //发布商品提交
  formSubmit(e) {
    var that = this;
    var goods_name = e.detail.value.goods_name;//商品名称 
    var goods_kuanhao = e.detail.value.goods_kuanhao;//商品款号
    var goods_remark = e.detail.value.goods_remark;//商品名称 
    
    var cost_price = parseFloat(e.detail.value.cost_price) || 0;//批发价
    var shop_price = parseFloat(e.detail.value.shop_price) || 0;//本店价
    var market_price = parseFloat(e.detail.value.market_price) || 0 ;//市场价
    var store_count = e.detail.value.store_count;//总库存
    var province = e.detail.value.province;
    var images = this.data.images;//上传文件
    var currentItem = this.data.currentItem;  //品牌id
    var fenggecurrentid = this.data.fenggecurrentid;  //风格id
    var goods_categoryid = this.data.goods_categoryid;//分类3级id
    var shuxing_list = this.data.shuxing_list;//属性集合
    var guigelist = this.data.guigelist;//规格集合

    if(goods_name.length<=0){
      //错误提示
      this.setData({
        showTopTipsText: '请输入商品名称',
        showTopTips: true
      })
      setTimeout(function () {
        that.setData({
          showTopTips: false
        });
      }, 3000);
    } else if (goods_remark.length <= 0) {
      //错误提示
      this.setData({
        showTopTipsText: '请输入商品简介',
        showTopTips: true
      })
      setTimeout(function () {
        that.setData({
          showTopTips: false
        });
      }, 3000);
    } else if (goods_kuanhao.length <= 0) {
      //错误提示
      this.setData({
        showTopTipsText: '请输入商品款号',
        showTopTips: true
      })
      setTimeout(function () {
        that.setData({
          showTopTips: false
        });
      }, 3000);
    }else if (cost_price == 0) {
      //错误提示
      this.setData({
        showTopTipsText: '请输入批发价',
        showTopTips: true
      })
      setTimeout(function () {
        that.setData({
          showTopTips: false
        });
      }, 3000);
    }else if (shop_price == 0){
      //错误提示
      this.setData({
        showTopTipsText: '请输入零售格',
        showTopTips: true
      })
      setTimeout(function () {
        that.setData({
          showTopTips: false
        });
      }, 3000);
    } else if (market_price == 0){
      //错误提示
      this.setData({
        showTopTipsText: '请输入吊牌价',
        showTopTips: true
      })
      setTimeout(function () {
        that.setData({
          showTopTips: false
        });
      }, 3000);
    } else if (shop_price < cost_price) {
      //错误提示
      this.setData({
        showTopTipsText: '零售价必须大于批发价',
        showTopTips: true
      })
      setTimeout(function () {
        that.setData({
          showTopTips: false
        });
      }, 3000);
    } else if (market_price < shop_price) {
      //错误提示
      this.setData({
        showTopTipsText: '吊牌价必须大于等于零售价',
        showTopTips: true
      })
      setTimeout(function () {
        that.setData({
          showTopTips: false
        });
      }, 3000);
    }  else if (that.data.goods_categoryid == 0) {
      //错误提示
      this.setData({
        showTopTipsText: '请选择商品类别',
        showTopTips: true
      })
      setTimeout(function () {
        that.setData({
          showTopTips: false
        });
      }, 3000);
    }else if (currentItem == 0) {
      //错误提示
      this.setData({
        showTopTipsText: '请选择一个品牌',
        showTopTips: true
      })
      setTimeout(function () {
        that.setData({
          showTopTips: false
        });
      }, 3000);
    } else if (fenggecurrentid == 0) {
      //错误提示
      this.setData({
        showTopTipsText: '请选择一个商品风格',
        showTopTips: true
      })
      setTimeout(function () {
        that.setData({
          showTopTips: false
        });
      }, 3000);
    } else if (images.length <= 0) {
      //错误提示
      this.setData({
        showTopTipsText: '请上传图片',
        showTopTips: true
      })
      setTimeout(function () {
        that.setData({
          showTopTips: false
        });
      }, 3000);
    }else{
      //发布商品调用接口
      add_goods(that, goods_categoryid, goods_name, goods_kuanhao, goods_remark, cost_price, market_price, shop_price, this.data.store_id, store_count, guigelist, shuxing_list, currentItem, images, fenggecurrentid) 
    }





  },





  //多选  选择颜色
  checkboxChange(e) {

    var values = e.detail.value
    var checkbox = this.data.chima3
    console.log(e.detail.value)
    var clist = []
    var c = 0
    for (let i = 0, lenI = checkbox.length; i < lenI; ++i) {
      checkbox[i].checked = !1
      for (let j = 0, lenJ = values.length; j < lenJ; ++j) {
        if (checkbox[i].id == values[j]) {
          checkbox[i].checked = !0
          var clist1 = {};
          clist1['id'] = checkbox[i].id;
          clist1['name'] = checkbox[i].item;
          clist[c] = clist1;
          c++;
          break
        }
      }
    }

    this.setData({
      chima3: checkbox,
      xuanyanse: values,
      xuanyanse_list:clist,
    })
    //console.log("选择的颜色ID" + this.data.xuanyanse_list);

 

    //动态规格列表添加
    if (this.data.xuanyanse_list.length > 0 && this.data.xuanchima.length>0){
      this.setData({
        is_show_chima:true,
      })
      //guigelist 列表中添加值
      //js 组合集合值到guigelist中
      var guigelist = []
      
      var k=0
      for(var i=0;i<this.data.xuanyanse.length;i++){
 
        for(var j=0;j<this.data.xuanchima.length;j++){
          var guigelist1 = {}
          guigelist1['ysid'] = this.data.xuanyanse_list[i].id;
          guigelist1['ysname'] = this.data.xuanyanse_list[i].name;
          guigelist1['cmid'] = this.data.xuanchima_list[j].id;
          guigelist1['cmname'] = this.data.xuanchima_list[j].name;
          guigelist1['jiage'] = 'jiage'+k;
          guigelist1['jiage_value'] = this.data.shop_price;
          guigelist1['kc'] = 'kc'+k;
          guigelist1['kc_value'] = 0;
          guigelist1['ggid'] = k+1;
          guigelist[k] = guigelist1;
          k++;
        }

      }
      this.setData({
        guigelist: guigelist,
      })
      console.log(guigelist)
    } else {
      this.setData({
        is_show_chima: false,
        guigelist: [],
      })
    }

  },

  //多选  选择尺码
  checkboxChange2(e) {

    var values = e.detail.value
    var checkbox = this.data.chima2
    console.log(e.detail.value)
    var clist = []
    var c = 0
    for (let i = 0, lenI = checkbox.length; i < lenI; ++i) {
      checkbox[i].checked = !1
      for (let j = 0, lenJ = values.length; j < lenJ; ++j) {
        if (checkbox[i].id == values[j]) {
          checkbox[i].checked = !0
          var clist1 = {};
          clist1['id'] = checkbox[i].id;
          clist1['name'] = checkbox[i].item;
          clist[c] = clist1;
          c++;
          break
        }
      }
    }

    this.setData({
      chima2: checkbox,
      xuanchima: values,
      xuanchima_list:clist,
    })
    console.log("选择的尺码ID" + this.data.xuanchima);
    console.log(this.data.xuanchima[0]);

    //动态规格列表添加
    if (this.data.xuanyanse_list.length > 0 && this.data.xuanchima_list.length > 0) {
      this.setData({
        is_show_chima: true,
      })
      //guigelist 列表中添加值
      //js 组合集合值到guigelist中
      var guigelist = []
      
      var k = 0
      for (var i = 0; i < this.data.xuanyanse_list.length; i++) {

        for (var j = 0; j < this.data.xuanchima_list.length; j++) {
          var guigelist1 = {}
          guigelist1['ysid'] = this.data.xuanyanse_list[i].id;
          guigelist1['ysname'] = this.data.xuanyanse_list[i].name;
          guigelist1['cmid'] = this.data.xuanchima_list[j].id;
          guigelist1['cmname'] = this.data.xuanchima_list[j].name;
          guigelist1['jiage'] = 'jiage' + k;
          guigelist1['jiage_value'] = this.data.shop_price;
          guigelist1['kc'] = 'kc' + k;
          guigelist1['kc_value'] = 0;
          guigelist1['ggid'] = k + 1;
          guigelist[k] = guigelist1;
          k++;
        }

      }
      this.setData({
        guigelist: guigelist,
      })
      console.log(guigelist)
    }else{
      this.setData({
        is_show_chima: false,
        guigelist:[],
      })
    }

  },
  //规格列表的价格 输入框失去焦点时触发事件
  onclickguigelistjiage:function(e){
    var values = e.detail.value
    console.log(values)
    var id = e.currentTarget.dataset.id
    console.log(id) 
    var guigelist = this.data.guigelist;
    for (var i = 0; i < guigelist.length;i++){
      if (id == guigelist[i].ggid){
        guigelist[i].jiage_value = values;
        
      }
    }
    this.setData({
      guigelist:guigelist,
    })
    console.log(this.data.guigelist)
  },
  //规格列表的库存 输入框失去焦点时触发事件
  onclickguigelistkc:function(e){
    var values = e.detail.value
    console.log(values)
    var id = e.currentTarget.dataset.id
    console.log(id) 
    var guigelist = this.data.guigelist;
    for (var i = 0; i < guigelist.length; i++) {
      if (id == guigelist[i].ggid) {
        guigelist[i].kc_value = values;
      }
    }
    this.setData({
      guigelist: guigelist,
    })
    console.log(this.data.guigelist)
  },

  onclickshop_price:function(e){
    var values = e.detail.value
    this.setData({
      shop_price:values,
    })
  },

  // 属性的 多选框事件 
  checkboxChange_shuxin:function(e){
    var values = e.detail.value
    var id = e.currentTarget.dataset.id
    var cklist = this.data.shuxing_list
  
    for(var i=0;i<cklist.length;i++){
      if(cklist[i].attr_id == id){
        for(var j=0;j<cklist[i].attr_values.length;j++){
          cklist[i].attr_values[j].checked = !1;
          for(var k=0;k<values.length;k++){
            if (cklist[i].attr_values[j].name == values[k]){
              cklist[i].attr_values[j].checked = !0;
              this.setData({
                spshuxinginput: cklist[i].attr_values[j].name+'...',
              })
            }
          }
          
        }
      }
    }
 
    this.setData({
      shuxing_list: cklist,
    })
    
    //console.log(this.data.xuanshuxing_list)


  },
  // 属性的  单选框事件
  checkboxChange_shuxin2: function (e) {
    var values = e.detail.value
    var id = e.currentTarget.dataset.id
    var cklist = this.data.shuxing_list

    for (var i = 0; i < cklist.length; i++) {
      if (cklist[i].attr_id == id) {
        for (var j = 0; j < cklist[i].attr_values.length; j++) {

          for (var k = 0; k < values.length; k++) {
            if (cklist[i].attr_values[j].name == values) {
              cklist[i].attr_values[j].checked = !0;
              this.setData({
                spshuxinginput: cklist[i].attr_values[j].name + '...',
              })
            }else{
              cklist[i].attr_values[j].checked = !1;
            }
          }

        }
      }
    }

    this.setData({
      shuxing_list: cklist,
    })

    //console.log(this.data.xuanshuxing_list)


  },

  //上传图片 显示功能
  chooseImage: function (e) {
    var that = this;

      wx.chooseImage({
        sizeType: 'compressed', // 可以指定是原图还是压缩图，默认二者都有
        sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
        success: function (res) {
          // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
          that.setData({
            //files: that.data.files.concat(res.tempFilePaths),
            files: res.tempFilePaths,
            images:[],//清空图片数据
            disabled:true,
          });
          //上传图片 递归方法
          sendPhotos(res.tempFilePaths, that, app.globalData.openid, app.globalData.user_id)
            
        }
      })
    
       
  },
  previewImage: function (e) {
    wx.previewImage({
      current: e.currentTarget.dataset.id, // 当前显示图片的http链接
      urls: this.data.files // 需要预览的图片http链接列表
    })
  },

  //提示弹出框事件
  showyingxiao:function(){
    this.setData({
      showyingxiao: !this.data.showyingxiao,

    });
  },
  //弹出模拟框事件 打开/取消
  toggleModal() {
    this.setData({
      showModal: !this.data.showModal,
 
    });
  },
  //弹出模拟框事件 保存
  toggleModalsave:function(e){
    var that = this
    that.setData({
      showModal: false,

    });

  },
  //弹出框 滚动到底部触发事件
  upper:function(e){

    wx.showToast({
      title: '到底了',
      icon: 'success',
      duration: 1000
    })
  },

  //隐藏品牌
  yingcangpp:function(e){
    var that = this;
    that.setData({
      is_show_pingpai:false,
    })
  },
  //隐藏风格
  yingcangfg: function (e) {
    var that = this;
    that.setData({
      is_show_fengge: false,
    })
  },
  //添加规格
  showguige: function () {
    var that = this;
    that.setData({
      is_show_guige: true,
    })


  },
  //添加品牌
  showpingpai:function(){
    var that = this;
    that.setData({
      is_show_pingpai: true,
    })
  },


  //点击 品牌事件
  onclickpingpai: function (e) {
    var id = e.currentTarget.dataset.id;
    var name = e.currentTarget.dataset.name;
    this.setData({
      currentItem: id,
      shop_zpingpai: '',
      sppp:name,
    })
    console.log(id)

  },
  //点击 风格事件
  onclickfengge: function (e) {
    var id = e.currentTarget.dataset.id;
    var name = e.currentTarget.dataset.name;
    this.setData({
      fenggecurrentid:id,
    })

  },
  // // 批发价 文本框输入事件
  // onclickcost_price: function (e) {
  //   console.log(e.detail.value);
  //   var that = this;
  //   that.setData({
  //     cost_price: e.detail.value
  //   })
  // },
  // //本店售价 文本框输入事件
  // onclickshop_price:function(e){
  //   console.log(e.detail.value);
  //   var that = this;
  //   that.setData({
  //     shop_price: e.detail.value
  //   })
  // },
  //自定义商品品牌事件
  onclickinputpingpai: function (e) {
    console.log(e.detail.value);
    var that = this;
    that.setData({
      shop_zpingpai: e.detail.value,
      currentItem: 0,
    })
  },
  //自定义规格颜色 输入框
  onclickinputzyanse:function(e){
    console.log(e.detail.value);
    var that = this;
    that.setData({
      shop_zyanse: e.detail.value,
      currentItem3: 0,
    })
  },
  //批量添加价格
  onclickguigelistpljg:function(e){
    var pljiage = e.detail.value
    var guigelist = this.data.guigelist;
    for (var i = 0; i < guigelist.length;i++){
      guigelist[i].jiage_value = pljiage
    }
    this.setData({
      guigelist: guigelist,
    })
  },
  //批量添加库存
  onclickguigelistplkc: function (e) {
    var plkc = e.detail.value
    var guigelist = this.data.guigelist;
    for (var i = 0; i < guigelist.length; i++) {
      guigelist[i].kc_value = plkc
    }
    this.setData({
      guigelist: guigelist,
    })
  },
  //自定义规格颜色 输入框 提交
  onclickyanse:function(e){
    var that = this;
    if(that.data.shop_zyanse.length <= 0){
      console.log("提交颜色没值")
      //错误提示
      this.setData({
        showTopTipsText: '请输入自定义颜色',
        showTopTips: true
      })
      setTimeout(function () {
        that.setData({
          showTopTips: false
        });
      }, 3000);
    }else{
      //提交自定义颜色到数据库
      //请求服务器
      wx.request({
        url: baseUrl+'weapp/Goods/add_goods_color.html',

        data: {
          store_id:that.data.store_id,//当前店铺ID
          color_name:that.data.shop_zyanse,//颜色名称
        },
        method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
        header: {
          'content-type': 'application/json'
        }, // 设置请求的 header
        success: function (res) {
          console.log(res.data)
          var yanse = res.data;
          for (var i = 0; i < yanse.length; i++) {
            yanse[i].checked = !1//集合添加元素
          }
          var chima3 = that.data.chima3;
          chima3.push(yanse)
          that.setData({
            chima3: chima3,
            shop_zyanse:'',
          })

        },
        fail: function () {
        },
        complete: function () {
          // complete
        }
      })
      

    }
  },
  //自定义品牌提交
  onclicksavepingpai:function(e){
    var that = this;
    if (that.data.shop_zpingpai.length <= 0) {
      
      //错误提示
      this.setData({
        showTopTipsText: '请输入自定义品牌',
        showTopTips: true
      })
      setTimeout(function () {
        that.setData({
          showTopTips: false
        });
      }, 3000);
    } else {
      //提交自定义颜色到数据库
      //请求服务器
      wx.request({
        url: baseUrl+'weapp/Goods/add_store_brand.html',

        data: {
          brand_name: that.data.shop_zpingpai,
          store_id: that.data.store_id,

        },
        method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
        header: {
          'content-type': 'application/json'
        }, // 设置请求的 header
        success: function (res) {
          console.log(res.data)
          var pingpai = that.data.pingpai
          pingpai.push(res.data);
          that.setData({
            pingpai: pingpai,
            shop_zpingpai:'',
          })
        },
        fail: function () {
        },
        complete: function () {
          // complete
        }
      })


    }
  },

  //自定义品牌删除
  onclickdelpingpai: function (e) {
    var that = this;
    if (that.data.shop_zpingpai.length <= 0) {
      console.log("提交品牌没值")
      //错误提示
      this.setData({
        showTopTipsText: '请输入自定义品牌',
        showTopTips: true
      })
      setTimeout(function () {
        that.setData({
          showTopTips: false
        });
      }, 3000);
    } else {
      wx.showModal({
        title: '提示',
        content: '您确定要删除该品牌吗',
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定')
            //提交自定义颜色到数据库
            //请求服务器
            wx.request({
              url: baseUrl +'weapp/Goods/delete_brand.html',

              data: {
                brand_name: that.data.shop_zpingpai,
                store_id: that.data.store_id,

              },
              method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
              header: {
                'content-type': 'application/json'
              }, // 设置请求的 header
              success: function (res) {
                console.log(res.data)
                if(res.data.code == 0){
                  console.log("删除品牌成功")
                  var pingpai = that.data.pingpai
                  for (var i = 0; i < pingpai.length;i++){
                    if (pingpai[i].name == that.data.shop_zpingpai){
                      pingpai.splice(i,1);
                      i = 0;
                    }
                  }
                  that.setData({
                    pingpai: pingpai,
                    shop_zpingpai: '',
                  })
                }else{
                  wx.showModal({
                    title: '提示',
                    content: '输入错误，没有该品牌',
                    showCancel: false,
                    success: function (res) {
                      if (res.confirm) {
                        console.log('用户点击确定')
                      } else if (res.cancel) {
                        console.log('用户点击取消')
                      }
                    }
                  })
                }
                
              },
              fail: function () {
              },
              complete: function () {
                // complete
              }
            })

          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })



    }
  },



}))

//上传图片 递归方法
function sendPhotos(arr, that, openid, user_id) {

  if (arr.length != 0) {
    wx.showToast({
      icon: "loading",
      title: "正在上传"
    })
    wx.uploadFile({
      url: baseUrl + 'weapp/SavePicApi/save_shoppic.html',
      filePath: arr[0],
      name: 'file',
      formData: {
        //和服务器约定的token, 一般也可以放在header中
        'openid': openid,
        'user_id': user_id,
      },
      success: function (res) {
        var data = res.data
        console.log(res.data)
        var images = that.data.images;
        images.push(JSON.parse(res.data));
        that.setData({
          images: images,
        })
        arr.splice(0, 1)
        sendPhotos(arr, that, openid, user_id)
      }
    })
  } else {
    that.setData({
      disabled: false,
    })
  }
}
//上传图片
// function sendPhotos(arr, that, openid, user_id) {
//   wx.showToast({
//     icon: "loading",
//     title: "正在上传"
//   })
//   wx.uploadFile({
//     url: baseUrl +'weapp/SavePicApi/save_shoppic.html',
//     filePath: arr,
//     name: 'file',
//     formData: {
//       //和服务器约定的token, 一般也可以放在header中
//       'openid': openid,
//       'user_id': user_id
//     },
//     header: { "Content-Type": "multipart/form-data" },
//     success: function (res) {
//       //console.log(res.data)
//       console.log(JSON.parse(res.data));
//       //var images = JSON.parse(res.data);
//       var images = that.data.images;
//       images.push(JSON.parse(res.data));

//       that.setData({
//         images: images,
//       })
//     }
//   })
// }


//获取品牌数据
function getpingpai(that, store_id) {
  //请求服务器
  wx.request({
    url: baseUrl +'weapp/Goods/store_brand.html',

    data: {
      store_id: store_id,
    },
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    header: {
      'content-type': 'application/json'
    }, // 设置请求的 header
    success: function (res) {
      console.log(res.data)
      that.setData({
        pingpai:res.data
      })

    },
    fail: function () {
    },
    complete: function () {
      // complete
    }
  })
}
//获取商品尺码规格：（包含尺寸 尺码 颜色等） 参数：cate_id => 商品的顶级分类 如，男装女装鞋子 store_id => 当前店铺的ID  type     => 1:尺寸,2:尺码,3:颜色
function getguige(that, cate_id, store_id, type) {
  //请求服务器
  wx.request({
    url: baseUrl +'weapp/Goods/goods_size.html',

    data: {
      cate_id: cate_id,
      store_id:store_id,
      type:type,
    },
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    header: {
      'content-type': 'application/json'
    }, // 设置请求的 header
    success: function (res) {


      if(type == 1){
        that.setData({
          chima1: res.data.list,
        })
      } else if (type == 2){
        var chima2 = res.data.list;
        for (var i = 0; i < chima2.length; i++) {
          chima2[i].checked = !1//集合添加元素
        }
        that.setData({
          chima2: chima2,
        })

      } else if (type == 3){
        var chima3 = res.data.list;
        for (var i = 0; i < chima3.length;i++){
          chima3[i].checked= !1//集合添加元素
        }
        that.setData({
          chima3: chima3,
        })
      }else{}


    },
    fail: function () {
    },
    complete: function () {
      // complete
    }
  })
}

//获取商品颜色
function goods_color(that, cate_id, store_id) {
  //请求服务器
  wx.request({
    url: baseUrl +'weapp/Goods/goods_color.html',

    data: {
      cate_id: cate_id,
      store_id: store_id,
    },
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    header: {
      'content-type': 'application/json'
    }, // 设置请求的 header
    success: function (res) {
      console.log(res.data)
      

        var chima3 = res.data.list;
        for (var i = 0; i < chima3.length; i++) {
          chima3[i].checked = !1//集合添加元素
        }
        that.setData({
          chima3: chima3,
        })



    },
    fail: function () {
    },
    complete: function () {
      // complete
    }
  })
}

//获取商品分类
function getgoods_category(that,pid){
  //请求服务器
  wx.request({
    url: baseUrl +'weapp/Goods/goods_category.html',

    data: {
      pid: pid,
    },
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    header: {
      'content-type': 'application/json'
    }, // 设置请求的 header
    success: function (res) {
      console.log(res.data)
      that.setData({
        goods_category: res.data
      })

    },
    fail: function () {
    },
    complete: function () {
      // complete
    }
  })
}

//获取商品属性 cate_id => 最小分类ID store_id => 店铺ID
function getgoods_attr(that, cate_id, store_id) {
  //请求服务器
  wx.request({
    url: baseUrl +'weapp/Goods/goods_attr.html',

    data: {
      cate_id: cate_id,
      store_id: store_id
    },
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    header: {
      'content-type': 'application/json'
    }, // 设置请求的 header
    success: function (res) {
      
      console.log(res.data)
      that.setData({
        shuxing_list:res.data,
      })

    },
    fail: function () {
    },
    complete: function () {
      // complete
    }
  })
}


/** 发布商品
 * 参数：
        cat_id3 => 最小分类ID
        goods_name => 商品名称
        market_price => 市场价
        shop_price => 店铺价
        store_id => 店铺ID
        store_count => 总库存字段：
        guigelist => 规格集合
        xuanshuxing_list => 属性集合
        currentItem => 品牌id
        images => 图片集合
 */
//发布商品
function add_goods(that, cat_id3, goods_name, goods_kuanhao, goods_remark, cost_price, market_price, shop_price, store_id, store_count, guigelist, shuxing_list, currentItem, images, fenggecurrentid) {
  that.setData({
    disabled:true,
  })
  //请求服务器
  wx.request({
    url: baseUrl +'weapp/Goods/add_goods.html',

    data: {
      cat_id3: cat_id3,
      goods_name: goods_name,
      spu: goods_kuanhao,
      goods_remark: goods_remark,
      cost_price: cost_price,
      market_price: market_price,
      shop_price: shop_price,
      store_id: store_id,
      store_count: store_count,
      guigelist: guigelist,
      shuxing_list: shuxing_list,
      currentItem: currentItem,
      images:images,
      tags: fenggecurrentid,
    },
    method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    header: {
      'content-type': 'application/json'
    }, // 设置请求的 header
    success: function (res) {
      console.log("发布商品返回值")
      console.log(res.data)
      if (res.data.code == 0) {
        //成功提示
        wx.showModal({
          title: '提示',
          content: '提交成功',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')
              wx.switchTab({
                url: '../mine/mine',
              })
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      }else if(res.data.code == -1){
        wx.showModal({
          title: '提示',
          content: '商品创建失败！',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
        that.setData({
          disabled: false,
        })
      } else if (res.data.code == -3){
        wx.showModal({
          title: '提示',
          content: '请选择商品属性！',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
        that.setData({
          disabled: false,
        })
      } else if (res.data.code == -2) {
        wx.showModal({
          title: '提示',
          content: '价格不能高于市场价',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
        that.setData({
          disabled: false,
        })
      } else if (res.data.code == -4) {
        wx.showModal({
          title: '提示',
          content: '请上传商品图片！',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
        that.setData({
          disabled: false,
        })
      } else if (res.data.code == -5) {
        wx.showModal({
          title: '提示',
          content: '请添加商品规格！',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
        that.setData({
          disabled: false,
        })
      } else if (res.data.code == -6) {
        wx.showModal({
          title: '提示',
          content: '您尚未申请店铺！',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
        that.setData({
          disabled: false,
        })
      }else{
        //成功提示
        wx.showModal({
          title: '提示',
          content: res.data,
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')
              // wx.switchTab({
              //   url: '../mine/mine',
              // })
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }

        })
        that.setData({
          disabled: false,
        })
      }
      
    },
    fail: function () {
    },
    complete: function () {
      // complete
    }
  })
}

//判断商铺状态
function get_store_state(that,user_id){
  //请求服务器
  wx.request({
    url: baseUrl + 'weapp/Store/get_store_state.html',
    data: {
      user_id: user_id,
    },
    method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    header: {
      'content-type': 'application/json'
    }, // 设置请求的 header
    success: function (res) {
      console.log(res.data)
      if(res.data.code == -1){
        //提示
        wx.showModal({
          title: '提示',
          content: '店铺审核中，暂不能发布商品',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')
              wx.switchTab({
                url: '../mine/mine',
              })
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      } else if (res.data.code == -2){
        //提示
        wx.showModal({
          title: '提示',
          content: '店铺已经被关闭',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')
              wx.switchTab({
                url: '../mine/mine',
              })
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      } else if (res.data.code == -3){
        //提示
        wx.showModal({
          title: '提示',
          content: '尚未创建店铺',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')
              wx.switchTab({
                url: '../mine/mine',
              })
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      }else{
        //调用品牌数据
        getpingpai(that, that.data.store_id)
        //商品分类调用数据 pid=0
        getgoods_category(that, 0)
      }
     

    },
    fail: function () {
    },
    complete: function () {
      // complete
    }
  })
}

//获取风格list
function goods_tags(that,cat_id3) {
  //请求服务器
  wx.request({
    url: baseUrl + 'weapp/Goods/goods_tags.html',
    data: {
      cat_id3: cat_id3,
    },
    method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    header: {
      'content-type': 'application/json'
    }, // 设置请求的 header
    success: function (res) {
      console.log(res.data)
      if(res.data.code == 0){
        that.setData({
          fenggelist: res.data.list,
          is_show_fengge:true,
          fenggecurrentid: 0,
        })
      } else if (res.data.code == -1) {
        that.setData({
          fenggelist: [],
          is_show_fengge: false,
          fenggecurrentid:-1,
        })

      }
    },
    fail: function () {
    },
    complete: function () {
      // complete
    }
  })
}